//
//  XSBrandChartViewController.m
//  MeiLiYiChu
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSBrandChartViewController.h"
#import "XSCollectionReusableView.h"
#import "XSShopCollectionViewController.h"
#import "XSMianViewController.h"
@interface XSBrandChartViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UICollectionView * _collection;
    NSMutableArray * bigArr;
}
@end

@implementation XSBrandChartViewController

-(void)viewWillAppear:(BOOL)animated
{
    [XSHTTPManager getBrandChatWithComplection:^(NSMutableArray *array, NSMutableArray *array2, NSMutableArray *array3) {
        _brandArr =array;
        _labArr =array2;
        _imageArr =array3;
 
        [_collection reloadData];
    }];
    
    self.navigationController.navigationBarHidden =YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    NSArray * array =@[@]
    NSArray * firstArr =@[@"234808",@"234892",@"234863",@"234854",@"234887",@"234843",@"234839",@"234840",@"234848",@"234844",@"234859",@"234846",@"234875",@"234866",@"234842",@"234841",@"234838",@"234836",@"234811",@"234845",@"234856",@"234855",@"234853",@"234852",@"234851",@"234850",@"234849",@"234847",@"234860",@"234858",@"234862",@"234845"];;
    NSArray * secondArr =@[@"234876",@"234877",@"234878",@"234896"];
    NSArray * thiredArr=@[@"234882",@"234881",@"234890",@"234888",@"234893",@"234894",@"234895",@"234879"];
    NSArray * fourArr =@[@"234867",@"234869",@"234870"];
    NSArray * fiveArr =@[@"234872",@"234874",@"234871",@"234873"];
    bigArr =[NSMutableArray arrayWithObjects:firstArr,secondArr,thiredArr,fourArr,fiveArr, nil];
    
    XSView * view =[[XSView alloc]initWithBool:NO withString:@"品牌排行"];
    [self.view addSubview:view];
    UIButton * btn1 =(UIButton *)[view viewWithTag:1];
    [btn1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UICollectionViewFlowLayout * layout =[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize =CGSizeMake((self.view.bounds.size.width-20)/4, 80);
    layout.sectionInset =UIEdgeInsetsMake(0, 10, 0, 10);
    
    layout.headerReferenceSize =CGSizeMake(self.view.bounds.size.width, 44);
    layout.minimumLineSpacing =0;
    layout.minimumInteritemSpacing =0;
    
    _collection =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-40) collectionViewLayout:layout];
    _collection.delegate =self;
    _collection.dataSource =self;
    _collection.backgroundColor =backColor;
    [self.view addSubview:_collection];
    
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _brandArr.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray * array =_brandArr[section];

    return array.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor =[UIColor whiteColor];
    UIImageView * image =[cell.contentView viewWithTag:cell.tag+10];
    if (image ==nil) {
        image =[[UIImageView alloc]init];
        image.center =CGPointMake(cell.contentView.center.x, cell.contentView.center.y);
        image.bounds =CGRectMake(0, 0, cell.contentView.frame.size.width/1.5, cell.contentView.frame.size.height/3);
        [cell.contentView addSubview:image];
    }
    NSArray * array  =_imageArr[indexPath.section];
 
    [image sd_setImageWithURL:[NSURL URLWithString:array[indexPath.row]]];
    return cell;
}
-(void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
//自定义区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView * reusableView  =[collectionView    dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];

    UILabel * lab =(UILabel *)[reusableView viewWithTag:20];
    if (!lab)
    {
        lab =[[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width, 44)];
        lab.tag = 20;
        [reusableView addSubview:lab];
    }
    
    lab.text =_labArr[indexPath.section];

    
    return reusableView;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray * array =bigArr[indexPath.section];
    
//    XSMianViewController * mainVC =[[XSMianViewController alloc]init];
    
    
//    [mainVC goShopRequsertWithUrl:array[indexPath.row]];
    
    [self.delegate name11:array[indexPath.row] ];
    
    
    
    
    
    
//    self.hidesBottomBarWhenPushed =YES;
//    [self.navigationController pushViewController:shopVC animated:YES];
//    self.hidesBottomBarWhenPushed =NO;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
